package structure.stack;

import java.util.Stack;

public class getMinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public getMinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int n){
        if (minStack.isEmpty()){
            minStack.push(n);
        }else if (n<=minStack.peek()){
            minStack.push(n);
        }
        stack.push(n);
    }

    public int pop(){
        int n = stack.pop();
        if (n<=minStack.peek()){
            minStack.pop();
        }
        return n;
    }

    public int getMin(){
        if (minStack == null){
            throw new IllegalArgumentException("栈为空");
        }
        return minStack.peek();
    }

    public static void main(String[] args) {
        getMinStack getMinStack = new getMinStack();
        getMinStack.push(2);
        getMinStack.push(3);
        System.out.println(getMinStack.getMin());
    }

}
